package com.lqfan.autoAnswerQ.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqfan.autoAnswerQ.controller.importQuestions.PoiWordController;

import com.lqfan.autoAnswerQ.model.user.User;
import com.lqfan.autoAnswerQ.service.user.IUserService;
import com.lqfan.autoAnswerQ.utils.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登入、注册、修改密码
 * @author zmlin
 * @since 2020/4/9
 */
@RestController
@RequestMapping(value = "/user",produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "用户接口")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(PoiWordController.class);
    @Autowired
    IUserService userService;

    //注册
    @ApiOperation(value = "注册",notes = "注册")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "phone",value = "预留手机号", dataType = "String")
            }
    )
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ResponseMessage registerUser(@RequestParam(value = "userName") String userName,
                                       @RequestParam(value = "password") String password,
                                       @RequestParam(value = "phone") String phone){
        logger.info("开始进行注册");
        boolean registerUser =userService.saveUser(userName,password,phone);
        if (registerUser){
            return ResponseMessage.success("注册成功");
        }else {
            return ResponseMessage.fail("用户已存在，注册失败");
        }

    }

    @ApiOperation(value = "登录" , notes = "登录")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String"),
            }
    )
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public ResponseMessage login(@RequestParam(value = "userName") String userName,
                                 @RequestParam(value = "password") String password
                                ){
        logger.info("开始进行登录");
        boolean login =userService.checkLogin(userName,password);
        if (login){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("userName",userName);
            User user = userService.getOne(queryWrapper);
            return ResponseMessage.success(user.getId());
        }else {
            return ResponseMessage.fail("登录失败，账号或密码错误");
        }
    }

    @ApiOperation(value = "修改密码" , notes = "修改密码")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "String"),
            }
    )
    @RequestMapping(value = "updatePass",method = RequestMethod.POST)
    public ResponseMessage updatePass(@RequestParam(value = "userName") String userName,
                                      @RequestParam(value = "phone") String phone,
                                 @RequestParam(value = "password") String password
    ){
        logger.info("开始修改密码");
        boolean updatePass =userService.updatePass(userName,phone,password);
        if (updatePass){
            return ResponseMessage.success("修改密码成功");
        }else {
            boolean value = userService.findUser(userName);
            if (!value) {
                return ResponseMessage.fail("修改密码失败，账号不存在");
            }
            return ResponseMessage.fail("修改密码失败");
        }

    }


}
